/*
 * UnitAnimal.cpp
 *
 *  Created on: 2013-1-19
 *      Author: heqiu
 */

#include "UnitAnimal.h"

namespace guud {
UnitAnimal::UnitAnimal() {
	m_pGuudSurface = NULL;
	m_iId = 0;
	m_iType = 0;
	m_uiBeginMSec = 0;
	m_uiSpeed = 1;
	m_fXshift = 0;
	m_fYshift = 0;

}
void UnitAnimal::setSurface(GuudSurface * pGuudSurface) {
	m_pGuudSurface = pGuudSurface;
}
void UnitAnimal::moving(uint32_t uiMSec, int32_t & iX, int32_t & iY) {
	if (m_uiBeginMSec == 0) {
		m_uiBeginMSec = uiMSec;
		m_fXshift = 0;
		m_fYshift = 0;
		iX = m_stPos.m_iX;
		iY = m_stPos.m_iY;
		return;
	}
	if (m_uiSpeed == 0) {
		iX = m_stPos.m_iX;
		iY = m_stPos.m_iY;
		return;
	}
	uint32_t uiNum = (uiMSec - m_uiBeginMSec) / m_uiSpeed;
	if (m_vctPath.size() > 0) {
		uint32_t uiPos = uiNum % m_vctPath.size();
		m_stPos.m_iX = m_vctPath.at(uiPos).m_iX;
		m_stPos.m_iY = m_vctPath.at(uiPos).m_iY;
		uint32_t uiShift = (uiNum + 1) % m_vctPath.size();
		float fShift = (uiMSec - m_uiBeginMSec) % m_uiSpeed * 1.0 / m_uiSpeed;
		if (m_vctPath.at(uiShift).m_iX < m_stPos.m_iX) {
			m_fXshift = -fShift;
		} else if (m_vctPath.at(uiShift).m_iX > m_stPos.m_iY) {
			m_fXshift = fShift;
		} else {
			m_fXshift = 0;
		}
		if (m_vctPath.at(uiShift).m_iY < m_stPos.m_iY) {
			m_fYshift = -fShift;
		} else if (m_vctPath.at(uiShift).m_iY > m_stPos.m_iY) {
			m_fYshift = fShift;
		} else {
			m_fYshift = 0;
		}
	}
	iX = m_stPos.m_iX;
	iY = m_stPos.m_iY;
}

}

